package br.com.hsp.system.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.hsp.users.Paciente;


public class PacienteRepository {
	
	private EntityManager manager ;
	
	public PacienteRepository ( EntityManager manager ) {
		this.manager = manager ;
	}
	
	public void add(Paciente paciente) {
		this.manager.persist(paciente);
	}
	
	public void remove(Long id) {
		Paciente paciente = this.procura(id);
		this.manager.remove(paciente);
	}
	
	public Paciente atualiza(Paciente paciente) {
		return this.manager.merge(paciente);
	}
	
	public Paciente procura(Long id) {
		return this.manager.find(Paciente.class, id);
	}
	
	public List<Paciente> getLista() {
		Query query = this.manager.createQuery("SELECT x from Paciente x");
		return query.getResultList();
	}
	
	public List<Paciente> getListaParaMedico(Long medicoId) {
		Query query = this.manager.createQuery("SELECT u from Usuario as u WHERE u.id IN(SELECT c.paciente.id from Consulta as c WHERE c.medico.id="+medicoId+")");
		return query.getResultList();
	}
	
	
	public Paciente procuraCPF(String cpf) {
		Query query = this.manager.createQuery("SELECT x from Paciente x WHERE x.cpf='"+cpf+"'");
		return (Paciente) query.getResultList().get(0);
	}
}
